﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.Util;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LianKe.DynamicFormPlugIn
{
    [Description("过滤方案动态表单插件"), HotUpdate]
    public class FilterListPlugIn : AbstractDynamicFormPlugIn
    {
        public override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string formId;
            DataTable dt;
            formId = this.View.OpenParameter.GetCustomParameter("formId").GetString();
            string sql = string.Format(@"/*dialect*/ 
            select FSCHEMENAME,FSCHEMEID from T_BAS_FILTERSCHEME where FFORMID='{0}'  AND FUSERID>0 ", formId);
            dt = DBUtils.ExecuteDataSet(this.Context, sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                Entity entity = this.View.BillBusinessInfo.GetEntity("F_POSW_FilterEntity");
                DynamicObjectCollection rows = this.Model.GetEntityDataObject(entity);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DynamicObject row = new DynamicObject(entity.DynamicObjectType);
                    //entity.SeqDynamicProperty.SetValue(row, i + 1);
                    row["F_POSW_FilterName"] = dt.Rows[i][0].ToString();
                    row["F_POSW_FilterId"] = dt.Rows[i][1].ToString();
                    rows.Add(row);
                }
            }
        }
        //public override void BeforeClosed(BeforeClosedEventArgs e)
        //{
        //    base.BeforeClosed(e);
        //}

        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            if (e.BarItemKey.Equals("POSW_tbReturnData"))
            {
                Entity entity = this.Model.BillBusinessInfo.GetEntity("F_POSW_FilterEntity");
                //获取单据体当前焦点行
                int curRowIndex = this.View.Model.GetEntryCurrentRowIndex("F_POSW_FilterEntity");
                var row = this.View.Model.GetEntityDataObject(entity, curRowIndex);
                this.View.ReturnToParentWindow(row);
                this.View.Close();
            }
        }
    }
}
